﻿
#region References

using System;
using System.Net.Mail;
using App.Core.Logging;

#endregion

namespace App.Core.Mail
{
    /// <summary>
    /// Reads messages from XML file
    /// </summary>
    public sealed class MailManager
    {
        #region SMTP Information
        public static String SmtpHost
        {
            get;
            set;
        }
        public static int SmtpPort
        {
            get;
            set;
        }
        #endregion SMTP Information

        #region Methods
        public static bool SendMail(string mailTo, string mailCc, string mailBcc, string mailFrom, string mailSubject, string mailBody)
        {
            try
            {
                using (MailMessage mailMessage = new MailMessage())
                {
                    mailMessage.From = new MailAddress(mailFrom);

                    //Spliting the to addresses by ','
                    string[] emailAddesses = mailTo.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string to in emailAddesses)
                    {
                        mailMessage.To.Add(new MailAddress(to.Trim()));
                    }

                    //Spliting the cc Adresses by ','
                    string[] ccAddresses = mailCc.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string cc in ccAddresses)
                    {
                        mailMessage.CC.Add(new MailAddress(cc.Trim()));
                    }

                    //determining the BCC of the mail.
                    string[] bccAddresses = mailBcc.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string bcc in bccAddresses)
                    {
                        mailMessage.Bcc.Add(new MailAddress(bcc.Trim()));
                    }

                    mailMessage.Subject = mailSubject;
                    mailMessage.Body = mailBody;
                    mailMessage.IsBodyHtml = true;

                    //sending the mail.
                    SmtpClient smtpClient = new SmtpClient();
                    smtpClient.Host = SmtpHost; //smtpHost;
                    smtpClient.Port = SmtpPort; //smtpPort;
                    smtpClient.Send(mailMessage);
                    return true;
                }
            }
            catch (Exception ex)
            {
                //AppLogger.LogInfo("Unable to Send Email. For more details see the next error.");
                AppLogger.LogException(ex);
            }
            return false;
        }
        #endregion
    }
}
